package network
{
	import flash.utils.getTimer;
	
	import utils.NumberTool;
	public class GetDownLoadSpeed
	{
		private static var size:Number;
		private static var lastTime:int=-1;
		private static var callBack:Function;
		public function GetDownLoadSpeed()
		{
		}
		
		public static function start(_call:Function):void
		{
			callBack=_call;
			size=0;
			lastTime=-1;
		}
		
		public static function getSize(value:Number):void
		{
			if(lastTime==-1)
			{
				lastTime=getTimer();
				
			}
			else
			{
				var nowTime:int=getTimer();
				if(nowTime-lastTime>=1000)
				{
					var lastSize:int=size;
					var nowSize:int=size+value;
				}
			}
			size+=value;
		}
		
		public static function getOffset(cb:int,time:int):void
		{
			var result:Number=cb/time;
			var size:String="";
			if(result<1024)
			{
				size=result+"b";
			}
			else if(result>=1024)
			{
				if(result>=1048576)
				{
					size=NumberTool.getFileSize(result.toString(),"B","M",1)+"M";
				}
				else
				{
					size=NumberTool.getFileSize(result.toString(),"B","K",1)+"K";
				}
			}
			callBack(size+"/S");
		}
		
	}
}